
package com.erp.common.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.select.beans.SelectDelegate;
import com.erp.framework.controller.form.SimpleBaseForm;
import com.erp.framework.util.user.User;

public class ActionTemplate {

    private ActionMapping mapping;

    private ActionForm formIn;

    private HttpServletRequest request;

    private HttpServletResponse response;

    private ActionErrors errors;

    public ActionTemplate() {
        super();
    }

    public ActionTemplate(ActionMapping mapping, ActionForm formIn, HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
        super();
        this.setMapping(mapping);
        this.setFormIn(formIn);
        this.setRequest(request);
        this.setResponse(response);
        this.setErrors(errors);
    }

    public ActionErrors getErrors() {
        return errors;
    }

    public void setErrors(ActionErrors errors) {
        this.errors = errors;
    }

    public SimpleBaseForm getBaseForm() {
        return (SimpleBaseForm) formIn;
    }

    public void setFormIn(ActionForm formIn) {
        this.formIn = formIn;
    }

    public ActionMapping getMapping() {
        return mapping;
    }

    public void setMapping(ActionMapping mapping) {
        this.mapping = mapping;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }
    public User getEditingUser() {
        return (User) this.getSession().getAttribute(ProjectConstants.SESSION_USER);
    }

    public HttpSession getSession() {
        return this.getRequest().getSession(false);
    }

    public String getPageEvent() {
        return this.getBaseForm().getPageEvent();
    }

    public ActionForward makeActionForwardToInput() {
        return new ActionForward(this.getMapping().getInput());
    }

    public void setResponseHeaderForDownload(String contentType, String filename) {
        this.getResponse().setContentType(contentType);
        this.getResponse().setHeader("Content-Disposition", "attachment;filename=\"" +  filename + "\"");
        this.getResponse().setHeader("Cache-control", "private");
        this.getResponse().setHeader("Pragma", null);
        this.getResponse().setStatus(HttpServletResponse.SC_OK);
    }

    public void setResponseHeaderForPDFDownload(String filename) {
        this.setResponseHeaderForDownload("application/pdf", filename);
    }

    public boolean hasErrors() {
        return this.getErrors().size() > 0;
    }

    public SelectDelegate makeSelectDelegate() {
        return new SelectDelegate();
    }

    public RemarkTextPopup makeRemarkTextPopup() {
        return new RemarkTextPopup(this.getSession());
    }

}
